package com.csust.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csust.reggie.common.CustomException;
import com.csust.reggie.domain.Setmeal;
import com.csust.reggie.domain.SetmealDish;
import com.csust.reggie.dto.SetmealDto;
import com.csust.reggie.mapper.SetmealMapper;
import com.csust.reggie.service.ISetmealDishService;
import com.csust.reggie.service.ISetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ISetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {

    @Autowired
    private ISetmealDishService setmealDishService;

    /**
     *  新增套餐
     * @param setmealDto
     */
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        // 保存套餐信息
        this.save(setmealDto);

        // 获取当前添加的套餐id
        Long id = setmealDto.getId();
        // 保存菜品信息

        //1. 获取所有的菜品和套餐
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        // 2. 设置SetmealDish的套餐id
        setmealDishes.stream().map((item) -> {
             item.setSetmealId(id);
             return item;
        }).collect(Collectors.toList());

        // 3. 添加
        setmealDishService.saveBatch(setmealDishes);

    }

    @Override
    public SetmealDto getByIdWithDish(Long id) {
        Setmeal s = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        // 复制属性
        BeanUtils.copyProperties(s,setmealDto);
        // 通过套餐id去获取当前的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    /**
     *  更新数据 现删除之前的数据
     * @param setmealDto
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        this.updateById(setmealDto);

        // 删除原来的菜品和套餐的关系
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        // 插入新的菜品和套餐的关系
        List<SetmealDish> setmealDishes =
                setmealDto.getSetmealDishes().stream().map((item) -> {
                    item.setSetmealId(setmealDto.getId());
                    return item;
                }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void removeWithDish(List<Long> ids) {
        // 判断当前删除的数据是否有正在预售的
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        int count = this.count(wrapper);

        // count > 0 表示有套餐在卖
        if (count > 0) {
            throw new CustomException("套餐正在售卖中，无法删除");
        }

        // 删除套餐数据，操作setmeal表，执行delete操作
        this.removeByIds(ids);

        // 删除套餐和菜品的关联
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }

    @Override
    public void updateSetmealStatus(int status, List<Long> ids) {
        // 先把要修改状态的查询出来之后
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        List<Setmeal> list = this.list(wrapper);

        // 在进行设置新的状态
        list.stream().map((item) -> {
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());

        // 批量修改操作
        this.updateBatchById(list);
    }
}
